﻿using System;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;

namespace PlurkApi
{
    /// <summary>
    /// the reponse of a plurk messgae
    /// </summary>
    public class response
    {
        /// <summary>
        /// the language of the plurk message
        /// </summary>
        public string lang { get; set; }

        /// <summary>
        /// The raw content as user entered it, useful when editing plurks or if you want to format the content differently. 
        /// </summary>
        public string content_raw { get; set; }

        /// <summary>
        /// The English qualifier, can be "says", show all: 
        /// loves likes shares gives hates wants has will asks wishes was feels thinks says is : freestyle hopes needs wonders
        /// </summary>
        public string qualifier { get; set; }

        /// <summary>
        /// The formatted content, emoticons and images will be turned into IMG tags etc. 
        /// </summary>
        public string content { get; set; }

        /// <summary>
        /// the user id of the responser
        /// </summary>
        public int user_id { get; set; }

        /// <summary>
        /// the user id of the responser
        /// </summary>
        public int id { get; set; }

        /// <summary>
        /// the responsed time
        /// </summary>
        public DateTime posted { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="response"/> class.
        /// </summary>
        /// <param name="jsonString">The json string.</param>
        public response(string jsonString)
        {
            var obj = JObject.Parse(jsonString);
            if (obj.Count <= 0) { return; }

            lang = (obj["lang"] == null) ? string.Empty : (string)obj["lang"];
            content_raw = (obj["content_raw"] == null) ? string.Empty : (string)obj["content_raw"];
            qualifier = (obj["qualifier"] == null) ? string.Empty : (string)obj["qualifier"];
            content = (obj["content"] == null) ? string.Empty : (string)obj["content"];

            user_id = (obj["user_id"] == null) ? 0 : (int)obj["user_id"];
            id = (obj["id"] == null) ? 0 : (int)obj["id"];

            if (obj["posted"] != null) { posted = DateTime.Parse(obj["posted"].ToString().Replace("\"", "")); }
        }
    }

    /// <summary>
    /// a collection responses 
    /// </summary>
    public class responses : List<response> { }
}